// ActionScript file
import com.founder.framework.common.FounderUtils;
import com.founder.framework.common.StringUtils;
import com.qjz.controller.SchoolInfoController;
import com.qjz.controller.StudentInfoController;
import com.qjz.entity.StudentInfoQueryParameter;
import com.qjz.model.ListenerModel;
import com.qjz.model.LoginUserModel;
import com.qjz.model.SchoolInfoModel;
import com.qjz.model.StudentInfoModel;
import com.qjz.view.ui.HFullSch;

import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.core.FlexGlobals;
import mx.events.FlexEvent;
import mx.managers.PopUpManager;

[Bindable]
private var model:StudentInfoModel;
private var controller:StudentInfoController;
[Bindable]
private var schoolModel:SchoolInfoModel;
private var schoolCtrl:SchoolInfoController;
[Bindable]
private var studentModel:StudentInfoModel;
private var studentCtrl:StudentInfoController;
private var _instance:LoginUserModel=LoginUserModel.getInstance();
[Bindable]
private var defaultObj:Object=new Object();

protected function group1_creationCompleteHandler(event:FlexEvent):void
{
	// TODO Auto-generated method stub
	model=new StudentInfoModel();
	controller=new StudentInfoController(model);	
	controller.getDistinctList();
	
	defaultObj.item="-请选择-";
	if(_instance.user.leiXing == 9)
	{
		this.currentState="state1";
		schoolModel=new SchoolInfoModel();
		schoolCtrl=new SchoolInfoController(schoolModel);
		schoolCtrl.doSearch(schoolModel.schoolInfoQueryParameter);
	}
	else
	{
		this.currentState="state2";
		studentModel=new StudentInfoModel();
		studentCtrl=new StudentInfoController(studentModel);
		studentModel.studentInfoQueryParameter.studentInfo.xueXiaoDaiMa=_instance.user.xueXiaoId.toString();
		studentCtrl.getBanJiList(_instance.user.xueXiaoId.toString());
		
		ListenerModel.getInstance().addEventListener(ListenerModel.UPLOADE_SUCCESS, referenBanJiList);
	}
}

protected function btnSearch_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	doSearch(1, pagingBar.pageSize);
}

protected function pagingFunction(pageNumber:int, pageSize:int):void
{
	doSearch(pageNumber, pageSize);
}

private function doSearch(pageNumber:int, pageSize:int):void
{
	model.studentInfoQueryParameter=new StudentInfoQueryParameter();
	model.studentInfoQueryParameter.pageSize=pageSize;
	model.studentInfoQueryParameter.pageNumber=pageNumber;
	model.studentInfoQueryParameter.studentInfo.isDelete=0;
	if(!StringUtils.isNullOrEmpty(txtXingMing.text))
		model.studentInfoQueryParameter.studentInfo.xingMing=StringUtils.trim(txtXingMing.text);
	if(this.currentState=="state2")
	{
		model.studentInfoQueryParameter.studentInfo.xueXiaoDaiMa=_instance.user.xueXiaoId.toString();
		if(ddlBanJi.selectedIndex != 0)
			model.studentInfoQueryParameter.studentInfo.banJi=ddlBanJi.selectedItem.item;
	}
	else
	{
		if(ddlSchool.selectedItem != null)
		{
			model.studentInfoQueryParameter.studentInfo.xueXiaoDaiMa=ddlSchool.selectedItem.sid;
		}
		
		if(ddlRuXueNian.selectedIndex != 0)
			model.studentInfoQueryParameter.studentInfo.ruXueNian=ddlRuXueNian.selectedItem.item;
	}
	
	controller.doSearch(model.studentInfoQueryParameter);
}

protected function btnExport_clickHandler(event:MouseEvent):void
{
	model.studentInfoQueryParameter.studentInfo.isDelete=0;
	if(_instance.user.leiXing != 9)
	{
		model.studentInfoQueryParameter.studentInfo.xueXiaoDaiMa=_instance.user.xueXiaoId.toString();
		if(ddlBanJi.selectedIndex != 0)
			model.studentInfoQueryParameter.studentInfo.banJi=ddlBanJi.selectedItem.item;
	}
	else if(ddlSchool.selectedItem != null)
	{
		model.studentInfoQueryParameter.studentInfo.xueXiaoDaiMa=ddlSchool.selectedItem.sid;
	}
	controller.doExportData(model.studentInfoQueryParameter);
}

protected function lbHSch_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	var hSch:HFullSch=PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, HFullSch, true) as HFullSch;
	model.studentInfoQueryParameter=new StudentInfoQueryParameter();
	model.studentInfoQueryParameter.pageSize=pagingBar.pageSize;
	model.studentInfoQueryParameter.pageNumber=1;
	model.studentInfoQueryParameter.studentInfo.isDelete=0;
	if(_instance.user.leiXing != 9)
	{
		model.studentInfoQueryParameter.studentInfo.xueXiaoDaiMa=_instance.user.xueXiaoId.toString();
		hSch.currentState="state2";
		hSch.dataList=ddlBanJi.dataProvider as ArrayCollection;
	}
	else
	{
		hSch.dataList=ddlSchool.dataProvider as ArrayCollection;
	}
	hSch.model=this.model;
	PopUpManager.centerPopUp(hSch);
}

private function referenBanJiList(event:Event):void
{
	studentCtrl.getBanJiList(_instance.user.xueXiaoId.toString());
}